package com.thinkgem.jeesite.modules.cloudfactory.web;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.support.json.JSONUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.UserUtil;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryBuycar;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryFunction;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryMenuCenter;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryUser;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryUserFactory;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactroyOrder;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactoryBuycarService;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactoryFunctionService;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactoryMenuCenterService;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactoryUserFactoryService;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactroyOrderService;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 
* @ClassName: OrderController
* @Description: 订单中心（订单前台所有操作）
* @author Stone
* @date 2018年9月8日
*
 */
@RequiresPermissions("user")
@Controller
@RequestMapping("${adminPath}/order")
public class OrderController {
	
	@Autowired
	private CloudfactoryFunctionService cloudfactoryFunctionService;
	@Autowired
	private CloudfactoryUserFactoryService cloudfactoryUserFactoryService;
	@Autowired
	private CloudfactoryBuycarService cloudfactoryBuycarService;
	@Autowired
	private CloudfactoryMenuCenterService cloudfactoryMenuCenterService;
	@Autowired
	private CloudfactroyOrderService cloudfactroyOrderService;
	
	
	
	
	
	
	/**
	 * 
	* @Title: goOrderList
	* @Description: 跳转到我的所有订单
	* @param @param model
	* @param @param request
	* @param @param flag
	* @param @param functionType
	* @param @param orderNo
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goOrderList", method=RequestMethod.GET)
	public String goOrderList(Model model,HttpServletRequest request) {
		CloudfactoryUser cloudfactoryUser = UserUtil.getSessionUser(request);
		User user = new User();
		user.setId(cloudfactoryUser.getId());
		CloudfactroyOrder cloudfactroyOrder = new CloudfactroyOrder();
		cloudfactroyOrder.setUser(user);
		List<CloudfactroyOrder> orders = cloudfactroyOrderService.findList(cloudfactroyOrder);
		
		
		List<CloudfactroyOrder> count = cloudfactroyOrderService.count(cloudfactroyOrder);
		model.addAttribute("orders",orders);
		model.addAttribute("count",count);
		return "cloudfactory/orderlist";
	}
	/**
	 * 
	* @Title: goCloudMarket
	* @Description: 进入云市场
	* @param @param model
	* @param @param request
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goCloudMarket", method=RequestMethod.GET)
	public String goCloudMarket(Model model,HttpServletRequest request,String num,String orderNo) {
		model.addAttribute("isLogin",1);
		model.addAttribute("num",num);
		model.addAttribute("orderNo",orderNo);
		return "cloudfactory/proList";
	}
	
	/**
	 * 
	* @Title: goFuncitonList
	* @Description: 跳转到功能列表
	* @param @param model
	* @param @param request
	* @param @param flag 对应数据字典中的 center_type  功能表的 menu_type
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goFuncitonList", method=RequestMethod.GET)
	public String goFuncitonList(Model model,HttpServletRequest request,String flag,String functionType,String orderNo) {
		model.addAttribute("flag",flag);
		model.addAttribute("num",functionType);
		CloudfactoryFunction cloudfactoryFunction = new CloudfactoryFunction();
		cloudfactoryFunction.setMenuId(flag);
		cloudfactoryFunction.setIsRequired("0");
		cloudfactoryFunction.setFunctionType(functionType);
		List<CloudfactoryFunction> basics = cloudfactoryFunctionService.findList(cloudfactoryFunction);
		Integer basicsMoney = 0;
		String meunId = "";
		for(CloudfactoryFunction b : basics) {
			String mon = b.getMoney();
			if(!StringUtils.isBlank(mon)) {
				basicsMoney = basicsMoney+Integer.valueOf(mon);
				meunId = b.getMenuId();
			}
		}
		String introduce = "";
		if(!StringUtils.isBlank(meunId)) {
			CloudfactoryMenuCenter menu = cloudfactoryMenuCenterService.get(meunId);
			if(menu != null) {
				introduce = menu.getMenuDescribe();
			}
		}
		model.addAttribute("introduce",introduce);
		model.addAttribute("one",basicsMoney);
		model.addAttribute("six",basicsMoney*6);
		model.addAttribute("twelve",basicsMoney*12);
		
		model.addAttribute("basics",basics);//基础
		cloudfactoryFunction = new CloudfactoryFunction();
		cloudfactoryFunction.setMenuId(flag);
		cloudfactoryFunction.setIsRequired("1");
		cloudfactoryFunction.setFunctionType(functionType);
		model.addAttribute("apolegamy",cloudfactoryFunctionService.findList(cloudfactoryFunction));//选配
		
		CloudfactoryUser cloudfactoryUser = UserUtil.getSessionUser(request);
		User user = new User();
		user.setId(cloudfactoryUser.getId());
		CloudfactoryUserFactory cloudfactoryUserFactory = new CloudfactoryUserFactory();
		cloudfactoryUserFactory.setUser(user);
		List<CloudfactoryUserFactory> factory = cloudfactoryUserFactoryService.findList(cloudfactoryUserFactory);
		model.addAttribute("factory",factory);
		model.addAttribute("factoryNum",factory.size());
		if(StringUtils.isBlank(orderNo)) {
			model.addAttribute("orderNo",UUID.randomUUID().toString().replaceAll("-", "").substring(0, 11).toUpperCase());
		} else {
			model.addAttribute("orderNo",orderNo);
		}
		
		
		return "cloudfactory/prodetail";
	}
	
	/**
	 * 
	* @Title: saveBuyCar
	* @Description: 加入清单
	* @param @param request
	* @param @param response
	* @param @param model
	* @param @param id
	* @param @param orderNo
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "/saveBuyCar", method=RequestMethod.POST )
	@ResponseBody
	public String saveBuyCar(HttpServletRequest request,
			HttpServletResponse response, Model model,String id,String orderNo) {
		
			String success = cloudfactoryBuycarService.saveBuyCar(request,model,id,orderNo);

		return success;
	}
	/**
	 * 
	* @Title: deleBuyCar
	* @Description: 删除清单中的某一条
	* @param @param request
	* @param @param response
	* @param @param model
	* @param @param id
	* @param @param orderNo
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "/deleBuyCar", method=RequestMethod.POST )
	@ResponseBody
	public String deleBuyCar(HttpServletRequest request,
			HttpServletResponse response, Model model,String id,String orderNo) {
		
			String success = cloudfactoryBuycarService.deleBuyCar(request,model,id,orderNo);

		return success;
	}
	/**
	 * 
	* @Title: changeUseTime
	* @Description: 更改使用时长
	* @param @param request
	* @param @param response
	* @param @param model
	* @param @param id
	* @param @param orderNo
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "/changeUseTime", method=RequestMethod.POST )
	@ResponseBody
	public String changeUseTime(HttpServletRequest request,
			HttpServletResponse response, Model model,String id,String times) {
		
		return cloudfactoryBuycarService.changeUseTime(id,times,request);
	}
	/**
	 * 
	* @Title: insertBuyCar
	* @Description: 加入购物车
	* @param @param model
	* @param @param request
	* @param @param orderNo
	* @param @param factoryNo
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/insertBuyCar", method=RequestMethod.GET)
	public String insertBuyCar(Model model,HttpServletRequest request,String orderNo,String factoryId,String flag,String num,String jichu) {
		System.out.println("orderNo = " + orderNo);
		CloudfactoryUserFactory factory = cloudfactoryUserFactoryService.get(factoryId);
		String factoryNo = "";
		String factoryName = "";
		if(factory != null) {
			factoryNo = factory.getFactoryNo();
			factoryName = factory.getFactoryName();
		}
		CloudfactoryUser cloudfactoryUser = UserUtil.getSessionUser(request);
		User user = new User();
		user.setId(cloudfactoryUser.getId());
		CloudfactoryBuycar cloudfactoryBuycar = new CloudfactoryBuycar();
		cloudfactoryBuycar.setOrderNo(orderNo);
		cloudfactoryBuycar.setUser(user);
		List<CloudfactoryBuycar> cloudfactoryBuycars = cloudfactoryBuycarService.findList(cloudfactoryBuycar);
		CloudfactoryFunction cloudfactoryFunction = new CloudfactoryFunction();
		cloudfactoryFunction.setMenuId(flag);
		cloudfactoryFunction.setIsRequired("0");
		cloudfactoryFunction.setFunctionType(num);
		List<CloudfactoryFunction> factoryFunctions = cloudfactoryFunctionService.findList(cloudfactoryFunction);//基础
		Integer money = 0;
		for(CloudfactoryBuycar c : cloudfactoryBuycars) {
			String functionId = c.getFunctionId();
			 if(!StringUtils.isBlank(functionId)) {
				 
				 CloudfactoryFunction function = cloudfactoryFunctionService.get(functionId);
				 if(function != null) {
					 if(!StringUtils.isBlank(function.getMoney())) {
					 money = money+Integer.valueOf(function.getMoney());
					 }
				 }
			 }
		}
		for(CloudfactoryFunction c : factoryFunctions) {
			
		 money = money+Integer.valueOf(!StringUtils.isBlank(c.getMoney())?c.getMoney():"0");
				
		}
		for(CloudfactoryFunction c : factoryFunctions) {
			CloudfactroyOrder order = new CloudfactroyOrder();
			
			order.setUser(user);
			order.setOrderNo(orderNo);
			order.setMenuType(c.getFunctionType());
			order.setMenuId(c.getMenuId());
			CloudfactoryMenuCenter menu = cloudfactoryMenuCenterService.get(c.getMenuId());
			order.setMenuName(menu.getMenuName());
			order.setMenuDescribe(menu.getMenuDescribe());
			order.setFunctionName(c.getFunctionName());
			order.setIsRequired(c.getIsRequired());
			order.setFunctionId(c.getId());
			order.setFunctionDescribe(c.getOverview());
			order.setFunctionMoney(c.getMoney());
			order.setTotalMoney(String.valueOf(money));
			order.setUseTime(jichu);
			order.setUserName(cloudfactoryUser.getName());
			order.setCompanyName(cloudfactoryUser.getCompanyName());
			order.setStatus("3");
			order.setPlant(factoryNo);
			order.setFactroyName(factoryName);
			List<CloudfactroyOrder> or = cloudfactroyOrderService.findList(order);
			if(or==null || or.size()==0){
			cloudfactroyOrderService.save(order);
			}
		}
		for(CloudfactoryBuycar c : cloudfactoryBuycars) {
			CloudfactoryFunction funciton = cloudfactoryFunctionService.get(c.getFunctionId());
			
			CloudfactroyOrder order = new CloudfactroyOrder();
			
			order.setUser(user);
			order.setOrderNo(orderNo);
			order.setMenuType(funciton.getFunctionType());
			order.setMenuId(c.getMenuId());
			CloudfactoryMenuCenter menu = cloudfactoryMenuCenterService.get(c.getMenuId());
			order.setMenuName(menu.getMenuName());
			order.setMenuDescribe(menu.getMenuDescribe());
			order.setFunctionName(c.getFunctionName());
			order.setIsRequired(c.getIsRequired());
			order.setFunctionId(c.getFunctionId());
			order.setFunctionDescribe(funciton.getOverview());
			order.setFunctionMoney(funciton.getMoney());
			order.setTotalMoney(String.valueOf(money));
			order.setUseTime(c.getUseTimes());
			order.setUserName(cloudfactoryUser.getName());
			order.setCompanyName(cloudfactoryUser.getCompanyName());
			order.setStatus("3");
			order.setPlant(factoryNo);
			order.setFactroyName(factoryName);
			List<CloudfactroyOrder> or = cloudfactroyOrderService.findList(order);
			if(or==null || or.size()==0){
			cloudfactroyOrderService.save(order);
			}
		}
		CloudfactroyOrder cloudfactroyOrder = new CloudfactroyOrder();
		cloudfactroyOrder.setOrderNo(orderNo);
		List<CloudfactroyOrder> orders = cloudfactroyOrderService.findByOrderNo(cloudfactroyOrder);
		model.addAttribute("order",orders);
		model.addAttribute("money",money);
		model.addAttribute("orderNo",orderNo);
		model.addAttribute("spans",orders.size());
		model.addAttribute("num",num);
		model.addAttribute("orderStatus",orders!=null&&orders.size()>0?orders.get(0).getStatus():3);
		return "cloudfactory/shoppingCart";
	}
	
	
	@RequestMapping(value = "/submitOrder", method=RequestMethod.POST )
	@ResponseBody
	public String submitOrder(HttpServletRequest request,
			HttpServletResponse response, Model model,String orderNo) {
		CloudfactroyOrder cloudfactroyOrder = new CloudfactroyOrder();
		cloudfactroyOrder.setOrderNo(orderNo);
		List<CloudfactroyOrder> cloudfactroyOrders = cloudfactroyOrderService.findByOrderNo(cloudfactroyOrder);
		for(CloudfactroyOrder c : cloudfactroyOrders) {
			c.setStatus("0");
			CloudfactoryUser cloudfactoryUser = UserUtil.getSessionUser(request);
			c.setUserName(cloudfactoryUser.getLoginName());
			cloudfactroyOrderService.update(c);
		}
		return JSONUtils.toJSONString("");
	}
	
	/**
	 * 
	* @Title: lookBuyCar
	* @Description: 查看订单详情
	* @param @param model
	* @param @param request
	* @param @param orderNo
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/lookBuyCar", method=RequestMethod.GET)
	public String lookBuyCar(Model model,HttpServletRequest request,String orderNo) {
		CloudfactroyOrder cloudfactroyOrder = new CloudfactroyOrder();
		CloudfactoryUser cloudfactoryUser = UserUtil.getSessionUser(request);
		User user = new User();
		user.setId(cloudfactoryUser.getId());
		cloudfactroyOrder.setUser(user);
		cloudfactroyOrder.setOrderNo(orderNo);
		List<CloudfactroyOrder> orders = cloudfactroyOrderService.findByOrderNo(cloudfactroyOrder);
		Integer money = 0;
		String status ="";
		if(orders != null && orders.size()>0) {
			for(CloudfactroyOrder c : orders) {
				money = money+Integer.valueOf(!StringUtils.isBlank(c.getFunctionMoney())?c.getFunctionMoney():"0");
			}
			status = orders.get(0).getStatus();
		}
		
		model.addAttribute("orderStatus",status);
		model.addAttribute("order",orders);
		model.addAttribute("money",money);
		model.addAttribute("orderNo",orderNo);
		model.addAttribute("spans",orders.size());
		model.addAttribute("num",0);
		
		return "cloudfactory/shoppingCart";
	}
	
	@RequestMapping(value="/changeMonth", method=RequestMethod.GET)
	public String changeMonth(Model model,HttpServletRequest request,String orderNo,String id,String value) {
		
		
		CloudfactroyOrder order = cloudfactroyOrderService.get(id);
		order.setUseTime(value);
		String moneys = cloudfactoryFunctionService.get(order.getFunctionId()).getMoney();
		moneys = String.valueOf((Integer.valueOf(moneys)*Integer.valueOf(value)));
		
		order.setFunctionMoney(moneys);
		cloudfactroyOrderService.update(order);
		
		CloudfactroyOrder cloudfactroyOrder = new CloudfactroyOrder();
		CloudfactoryUser cloudfactoryUser = UserUtil.getSessionUser(request);
		User user = new User();
		user.setId(cloudfactoryUser.getId());
		cloudfactroyOrder.setUser(user);
		cloudfactroyOrder.setOrderNo(orderNo);
		List<CloudfactroyOrder> orders = cloudfactroyOrderService.findByOrderNo(cloudfactroyOrder);
		Integer money = 0;
		String status ="";
		if(orders != null && orders.size()>0) {
			for(CloudfactroyOrder c : orders) {
				money = money+Integer.valueOf(!StringUtils.isBlank(c.getFunctionMoney())?c.getFunctionMoney():"0");
			}
			status = orders.get(0).getStatus();
		}
		
		model.addAttribute("orderStatus",status);
		model.addAttribute("order",orders);
		model.addAttribute("money",money);
		model.addAttribute("orderNo",orderNo);
		model.addAttribute("spans",orders.size());
		model.addAttribute("num",0);
		
		return "cloudfactory/shoppingCart";
	}
	
	@RequestMapping(value="/deleOrder", method=RequestMethod.GET)
	public String deleOrder(Model model,HttpServletRequest request,String orderNo,String id) {
		
		
		CloudfactroyOrder order = cloudfactroyOrderService.get(id);
		cloudfactroyOrderService.delete(order);
		
		CloudfactroyOrder cloudfactroyOrder = new CloudfactroyOrder();
		CloudfactoryUser cloudfactoryUser = UserUtil.getSessionUser(request);
		User user = new User();
		user.setId(cloudfactoryUser.getId());
		cloudfactroyOrder.setUser(user);
		cloudfactroyOrder.setOrderNo(orderNo);
		List<CloudfactroyOrder> orders = cloudfactroyOrderService.findByOrderNo(cloudfactroyOrder);
		Integer money = 0;
		String status ="";
		if(orders != null && orders.size()>0) {
			for(CloudfactroyOrder c : orders) {
				money = money+Integer.valueOf(!StringUtils.isBlank(c.getFunctionMoney())?c.getFunctionMoney():"0");
			}
			status = orders.get(0).getStatus();
		}
		
		model.addAttribute("orderStatus",status);
		model.addAttribute("order",orders);
		model.addAttribute("money",money);
		model.addAttribute("orderNo",orderNo);
		model.addAttribute("spans",orders.size());
		model.addAttribute("num",0);
		
		return "cloudfactory/shoppingCart";
	}
}
